package com.ma.workorder;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.ma.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import base.BaseActivity;
import views.WriteDialogListener;
import views.WritePadDialog;

public class FeedbackActivity extends BaseActivity {
    private Bitmap mSignBitmap;
    private ImageView mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initView();
    }

    public void choose_result(View view) {

    }
    public void yes(View view) {
        showCustomDialog("确定保存","是否保存",view1 -> {},view1 -> {});

    }
    public void no(View view) {
        showCustomDialog("确定放弃","是否放弃",view1 -> {},view1 -> {});

    }

    public void writeName(View view) {

        WritePadDialog dialog = new WritePadDialog(this, new WriteDialogListener() {
            @Override
            public void onPaintDone(Object object) {
                Bitmap mSignBitmap = (Bitmap) object;
                createSignFile();

                mName.setImageBitmap(mSignBitmap);
            }
        });
        dialog.show();
    }

    private void createSignFile() {
        ByteArrayOutputStream baos = null;
        FileOutputStream fos = null;
        String path = null;
        File file = null;
        try {
            path = Environment.getExternalStorageDirectory() + File.separator + System.currentTimeMillis() + ".jpg";
            file = new File(path);
            fos = new FileOutputStream(file);
            baos = new ByteArrayOutputStream();
            //如果设置成Bitmap.compress(CompressFormat.JPEG, 100, fos) 图片的背景都是黑色的
            mSignBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] b = baos.toByteArray();
            if (b != null) {
                fos.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void initView() {
        mName = (ImageView) findViewById(R.id.name);
    }
}
